import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_yn_goods/config/app_color.dart';
import 'package:flutter_yn_goods/config/app_config.dart';
import 'package:flutter_yn_goods/config/app_translations.dart';
import 'package:flutter_yn_goods/utils/loading/loading_utils.dart';
import 'package:flutter_yn_goods/utils/shared/shared_storage.dart';
import 'package:flutter_yn_goods/widgets/button/common_button.dart';
import 'package:get/get.dart';

class WidgetLanguageDialog extends StatefulWidget {
  const WidgetLanguageDialog({super.key});

  Future<bool?> show() async {
    return await Get.dialog(
      this,
      // barrierDismissible: barrierDismissible,
      name: 'WidgetLanguageDialog',
      barrierColor: Colors.black.withOpacity(0.80),
      transitionDuration: const Duration(milliseconds: 250),
    );
  }

  @override
  State<WidgetLanguageDialog> createState() => _WidgetLanguageDialogState();
}

class _WidgetLanguageDialogState extends State<WidgetLanguageDialog> {
  Locale? currentLocale = SharedStorage().initialLocale() ?? Get.deviceLocale;

  void changeLanguage(Locale locale) {
    Get.updateLocale(locale);
    SharedStorage().setLocale(locale);
    AppConfig.setAppName();
    // GetStorage().write('language_code', locale.languageCode);
    // GetStorage().write('country_code', locale.countryCode ?? '');
  }

  @override
  Widget build(BuildContext context) {
    return _buildMain();
  }

  Widget _buildMain() {
    return Dialog(
      insetPadding: EdgeInsets.zero,
      backgroundColor: Colors.transparent,
      child: Container(
        margin: EdgeInsets.symmetric(horizontal: 44.w),
        padding: EdgeInsets.symmetric(vertical: 24.w, horizontal: 16.w),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(36.w),
          color: Colors.white,
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              LocaleKeys.change_language.tr,
              style: TextStyle(
                color: AppColor.color282828,
                fontSize: 18.sp,
                fontWeight: FontWeight.w600,
                height: 1.5,
              ),
            ),
            16.verticalSpace,
            _buildLanguage('中文', currentLocale?.languageCode == 'zh',
                onClick: () {
              currentLocale = const Locale('zh', 'CN');
            }),
            _buildLanguage('English', currentLocale?.languageCode == 'en',
                onClick: () {
              currentLocale = const Locale('en', 'US');
            }),
            _buildLanguage('Tiếng Việt', currentLocale?.languageCode == 'vi',
                onClick: () {
              currentLocale = const Locale('vi', 'VN');
            }),
            _buildLanguage('العربية', currentLocale?.languageCode == 'ar',
                onClick: () {
              currentLocale = const Locale('ar', 'SA');
            }),
            16.verticalSpace,
            Row(
              children: [
                Expanded(
                  child: (InkWell(
                    onTap: () {
                      Get.back();
                    },
                    child: Container(
                      height: 44.w,
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(24.w),
                          border: Border.all(
                              width: .5, color: AppColor.color999999)),
                      child: Text(
                        LocaleKeys.cancel.tr,
                        style: TextStyle(
                          color: AppColor.color999999,
                          fontSize: 14.sp,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ),
                  )).marginOnly(right: 12.w),
                ),
                Expanded(
                  child: CommonButton(
                    height: 44.w,
                    title: LocaleKeys.confirm.tr,
                    fontSize: 14.sp,
                    fontWeight: FontWeight.w600,
                    enable: true,
                    onClick: () {
                      if (currentLocale != null) {
                        changeLanguage(currentLocale!);
                        Get.back(result: true);
                      } else {
                        LoadingUtils.showToast(LocaleKeys.select_language.tr);
                      }
                      // Get.back(result: true);
                    },
                  ),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }

  Widget _buildLanguage(String title, bool isSelect,
      {required Function() onClick}) {
    return GestureDetector(
      onTap: () {
        onClick.call();
        if (mounted) {
          setState(() {});
        }
      },
      child: Container(
        margin: EdgeInsets.only(bottom: 12.w),
        height: 44.w,
        decoration: BoxDecoration(
            // color:
            color: isSelect ? AppColor.primary : Colors.transparent,
            borderRadius: BorderRadius.circular(12.w),
            border: Border.all(
              width: 1,
              color: isSelect ? Colors.transparent : AppColor.color282828,
            )),
        alignment: Alignment.center,
        child: Text(
          title,
          style: TextStyle(
              color: isSelect ? Colors.white : AppColor.color282828,
              fontSize: 14.sp,
              fontWeight: FontWeight.w500),
        ),
      ),
    );
  }
}
